using System;
using System.IO;
using ThoughtWorks.TreeSurgeon.Core.Generators;
using ThoughtWorks.TreeSurgeon.Core.Generators.Content;

namespace ThoughtWorks.TreeSurgeon.Core
{
	public class TreeSurgeonFrontEnd
	{
		private readonly IDirectoryBuilder directoryBuilder;
		private readonly IGenerator generator;
		private readonly string treeSurgeonApplicationDirectory;

		public TreeSurgeonFrontEnd(IDirectoryBuilder directoryBuilder, IGenerator generator, string treeSurgeonApplicationDirectory)
		{
			this.directoryBuilder = directoryBuilder;
			this.generator = generator;
			this.treeSurgeonApplicationDirectory = treeSurgeonApplicationDirectory;
		}

		public string GenerateDevelopmentTree(string projectName)
		{
			string outputPath = Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "TreeSurgeon"), projectName);
			directoryBuilder.CreateDirectory(outputPath);
			generator.Generate(projectName, outputPath, treeSurgeonApplicationDirectory);
			return outputPath;
		}

		// We don't have ObjectWizard plugged in yet, so here's our instance strategy
		public TreeSurgeonFrontEnd(string treeSurgeonApplicationDirectory) : this(
			new SimpleDirectoryBuilder(),
			new SimpleTreeGenerator(
				new DefaultDirectoryCopier(),
				new FileGeneratorWithTransformer(
					new LazilyInitialisingVelocityTransformer(new DefaultVelocityTransformerConfig(Path.Combine(treeSurgeonApplicationDirectory, "Resources\\templates")))
					),
				new StandardDotNetUpperCaseGuidGenerator()
				), treeSurgeonApplicationDirectory)
		{
		}
	}
}